package pl.edu.agh.mobicentsVoIP.database;

import java.util.Collection;
import java.util.HashSet;

import pl.edu.agh.mobicentsVoIP.core.Call;
import pl.edu.agh.mobicentsVoIP.core.CallStatus;
import pl.edu.agh.mobicentsVoIP.core.User;
import pl.edu.agh.mobicentsVoIP.utils.Logger;


public class MockFacade implements IFacade {

	private static MockFacade mockFacade = null;
	private HashSet<User> users;
	private HashSet<Call> calls;
	
	private MockFacade(){
		users = new HashSet<User>();
		calls = new HashSet<Call>();
	}
	
	public static MockFacade getInstance(){
		if(mockFacade == null){
			mockFacade = new MockFacade();
		}
		return mockFacade;
	}
	
	public void addCall(Call call) {
		calls.add(call);
	}

	public void registerUser(User user) {
		users.add(user);
		user.setRegistered(true);
	}

	public void unRegisterUser(User user) {
		user.setRegistered(false);
	}

	public Collection<Call> getAllCalls() {
		return calls;
	}

	public Collection<User> getAllUsers() {
		return users;
	}

	public Collection<Call> getAllUserCalls(User user) {
		HashSet<Call> userCalls = new HashSet<Call>();
		for(Call call : calls){
			if(user.equals(call.getFrom()) || user.equals(call.getTo())){
				userCalls.add(call);
			}
		}		
		return userCalls;
	}

	public Collection<User> getRegisteredUsers(boolean registered) {
		HashSet<User> filteredUsers = new HashSet<User>();
		for(User u : users){
			if(u.isRegistered() == registered){
				filteredUsers.add(u);
			}
		}
		return filteredUsers;
	}

	public User getUserByUserURI(String userURI) {
		for(User u : users){
			if(u.getUserURI().equals(userURI)){
				return u;
			}
		}
		return null;
	}

	public Call getCallByUsersURIs(String fromURI, String toURI) {
		for(Call call : calls){
			if(call.getStatus() == CallStatus.CONNECTED){
				if(call.getFrom().getUserURI().equals(fromURI) && call.getTo().getUserURI().equals(toURI)){
					return call;
				}
			}
		}
		return null;
	}
	
	public void logState(Logger logger){
		logger.info("-------------------------------------------------------------");
		logger.info("Users:");
		for(User u : users){
			logger.info(u.toString());
		}
		logger.info("-------------------------------------------------------------");
		logger.info("calls:");
		for(Call c : calls){
			logger.info(c.toString());
		}
		logger.info("-------------------------------------------------------------");
	}

	@Override
	public void updateCall(Call call) {}

}
